//
//  MeatOrderListViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/19.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderListViewController.h"
#import "MeatOrderDetailViewController.h"

#import "MeatPayView.h"

#import "MeatOrderBodyCell.h"
#import "MeatOrderHeaderCell.h"
#import "MeatOrderFooterCell.h"


@interface MeatOrderListViewController ()

@property (nonatomic, strong) MeatPayView *footer;

@property (nonatomic , strong) UIButton *detailTypeBtn;

@property (nonatomic , strong) NSMutableArray *transicationTypeArr;

@property (nonatomic , assign) TransicationType transicationType;

@end

@implementation MeatOrderListViewController


- (void)setupUI{
    
    {
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        UIImage *image = [UIImage imageNamed:@"meat_question_nav"];
        self.baseNavView.layer.contents =  (__bridge id)image.CGImage;
    }
    
    {
        [self.baseNavView.rightNavBtn setTitle:@"切换猪只" forState:UIControlStateNormal];        
        [self.baseNavView.rightNavBtn setTitle:@"赠送列表" forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.width = 120;
        [self.baseNavView.rightNavBtn sizeToFit];
        self.baseNavView.rightNavBtn.mj_y = TJ_NavBarHeight - 42;
        self.baseNavView.rightNavBtn.mj_x = (KScreenWidth - self.baseNavView.rightNavBtn.width)-15;
        [self.baseNavView.rightNavBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        self.baseNavView.rightNavBtn.isNeedAnimation = YES;

        
        __block BOOL selected = NO;
        @weakify(self);
        self.baseNavView.rightNavBtn.tapBlock = ^{
            @strongify(self);
            if (selected) {
                selected = NO;
                [self.baseNavView.rightNavBtn setTitle:@"编辑" forState:UIControlStateNormal];
                self.footer.alpha = 0;
            }else{
                selected = YES;
                [self.baseNavView.rightNavBtn setTitle:@"完成" forState:UIControlStateNormal];
                self.footer.alpha = 1;
            }
        };
        
        
        self.detailTypeBtn.mj_y = self.baseNavView.navTitleLabel.mj_y;
        self.detailTypeBtn.mj_x = (KScreenWidth - self.detailTypeBtn.width)/2.0;
        self.detailTypeBtn.height = self.baseNavView.navTitleLabel.height;
        [self.baseNavView addSubview:self.detailTypeBtn];
    }
    
    {
        [self.view addSubview:self.footer];
    }
    
}


- (void)setupLayout{
    
    self.footer.mj_y = self.view.bottom - _footer.height;
}

- (void)initData {
    
    NSArray *titleArr = @[@"全部订单",@"正常订单",@"预定单"];
    
    for (int i = 0; i < titleArr.count; i ++) {
        PullDownMenuModel *model = [[PullDownMenuModel alloc] init];
        // model.icon = iconArr[i];
        model.title = titleArr[i];
        model.isSelect = NO;
        [self.transicationTypeArr addObject:model];
    };
}


#pragma mark - target action
- (void)detailTypeClick:(UIButton *)button {
    button.selected = !button.selected;
    
    if (button.selected) {
        @weakify(self);
        [[PullDownMenu sharedInstance] showWithFrame:CGRectMake(0, TopBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) type:PullDownTypeTransicationTypes dataArr:self.transicationTypeArr completionBlock:^(NSString *text, NSInteger idx ,PullDownType pullWownType) {
            @strongify(self);
            if ([text isEqualToString:@"预定单"]) {
                self.transicationType = TransicationTypeAll;
            } else if ([text isEqualToString:@"充值"]){
                self.transicationType = TransicationTypeTopUp;
            } else if ([text isEqualToString:@"预定单"]){
                self.transicationType = TransicationTypeAdoptPig;
            } else if ([text isEqualToString:@"预售款"]){
                self.transicationType = TransicationTypePresell;
            } else if ([text isEqualToString:@"奖金"]){
                self.transicationType = TransicationTypeReward;
            } else if ([text isEqualToString:@"提现"]){
                self.transicationType = TransicationTypeWithdraw;
            } else if ([text isEqualToString:@"买肉"]) {
                self.transicationType = TransicationTypeBugMeat;
            }
            //修改title和img的edge
            //            [self.detailTypeBtn setTitle:text forState:UIControlStateNormal];
            //            [self changeBtnEdge];
            //            self.detailTypeBtn.selected = !self.detailTypeBtn.selected;
            //            [self.dataArr removeAllObjects];
            //            self.currentPage = 1;
            //[self dataRequest];
        }];
    } else {
        [[PullDownMenu sharedInstance] hide];
    }
}

#pragma mark - networking methods
#pragma mark - response methods
#pragma mark - public methods

#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击tableView");
    
    MeatOrderDetailViewController *vc = [[MeatOrderDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - tableview.datascource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
            return 51;
            break;
        case 1:
            return 35;
            break;
        case 4:
            return 35;
            break;
        case 5:
            return 49;
        
        default:
            return 71;
            break;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
            {
                MeatOrderHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderHeaderCell"];
                
                cell.dealOperationBlock = ^{
                    
                };
                
                return cell;
            }
            break;
            
        case 1:
            {
                BaseIndentationTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderComments"];
                cell.textLabel.text = @"订单备注:请发货前先与我联系下";
                cell.textLabel.textColor = UIColorFromRGB(0x454545);
                cell.textLabel.font = mFont(13);
                return cell;
            }
            break;
            
        case 4:
            {
                BaseIndentationTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderPrice"];
                cell.textLabel.text = @"共2件商品 合计：¥344";
                cell.textLabel.textAlignment = 2;
                return cell;

            }
            break;
            
            
        case 5:
            {
                MeatOrderFooterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderFooterCell"];
                return cell;
            }
            break;

        default:
            {
                MeatOrderBodyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderBodyCell"];
                cell.backgroundColor = KRedColor;
                return cell;

            }
            break;
    }
    
    return [UITableViewCell new];
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.height = KScreenHeight - TJ_NavBarHeight;
    self.tableView.mj_y = TJ_NavBarHeight;
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[MeatOrderHeaderCell class] forCellReuseIdentifier:@"MeatOrderHeaderCell"];
    [self.tableView registerClass:[MeatOrderFooterCell class] forCellReuseIdentifier:@"MeatOrderFooterCell"];
    [self.tableView registerClass:[MeatOrderBodyCell class] forCellReuseIdentifier:@"MeatOrderBodyCell"];
    [self.tableView registerClass:[BaseIndentationTableCell class] forCellReuseIdentifier:@"MeatOrderPrice"];
    [self.tableView registerClass:[BaseIndentationTableCell class] forCellReuseIdentifier:@"MeatOrderComments"];

}


- (MeatPayView *)footer{
    
    if (!_footer) {
        _footer = [[MeatPayView alloc] init];
        _footer.type = MeatPayType_SeparatePlan;
        _footer.alpha = 0;
        
        //全选Block
        @weakify(self);
        _footer.chooseCartBlock = ^(UIButton *btn) {
            
        };
        
        //预留
        _footer.clearCartBlock = ^{
            @strongify(self);
            [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"您确定要全部预留吗？，一经预留不可更改" confirmBtnTitle:@"确定" confirmBlock:^{
                // [self clearLoginAccount];
            } cancelBlock:nil];
        };
        
        //销售
        _footer.deleteCartBlock = ^{
            
        };
    }
    return _footer;
}


- (NSMutableArray *)transicationTypeArr{
    
    if (!_transicationTypeArr) {
        _transicationTypeArr = [[NSMutableArray alloc] init];
    }
    return _transicationTypeArr;
}


- (UIButton *)detailTypeBtn {
    
    if (!_detailTypeBtn) {
        _detailTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailTypeBtn.frame = CGRectMake(0, 0, 100, 0);
        [_detailTypeBtn setTitle:@"全部订单" forState:UIControlStateNormal];
        [_detailTypeBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        _detailTypeBtn.titleLabel.font = mFont(18);
        [_detailTypeBtn setImage:[UIImage imageNamed:@"meat_separate_up"] forState:UIControlStateNormal];
        [_detailTypeBtn setImage:[UIImage imageNamed:@"meat_separate_down"] forState:UIControlStateSelected];
        [_detailTypeBtn addTarget:self action:@selector(detailTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_detailTypeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - _detailTypeBtn.imageView.image.size.width, 0, _detailTypeBtn.imageView.image.size.width)];
        [_detailTypeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _detailTypeBtn.titleLabel.bounds.size.width, 0, -_detailTypeBtn.titleLabel.bounds.size.width)];
    }
    return _detailTypeBtn;
}



@end
